package ui.activity;

import android.app.DatePickerDialog;
import android.content.Intent;
import android.os.Build;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.RequiresApi;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.fy.fyzf.R;
import com.fy.fyzf.base.BaseActivity;
import com.fy.fyzf.bean.AddSignTwo;
import com.fy.fyzf.bean.PassengerNumberBean;
import com.fy.fyzf.bean.BaseData;
import com.fy.fyzf.bean.SignDetailBean;
import com.fy.fyzf.presenter.AddSignPresenter;
import com.fy.fyzf.utils.AppUtils;
import com.fy.fyzf.view.AddSignCallback;
import com.google.gson.Gson;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import ui.adapter.CheckBoxAdapter;

public class AddSignTwoActivity extends BaseActivity<AddSignPresenter> implements AddSignCallback {

    @BindView(R.id.tv_date)
    TextView tvDate;
    @BindView(R.id.tv_pay_type)
    EditText tvPayType;
    @BindView(R.id.tv_jiaofang_time)
    TextView tvJiaofangTime;
    @BindView(R.id.tv_rent)
    TextView tvRent;
    @BindView(R.id.et_owner_name)
    EditText etOwnerName;
    @BindView(R.id.et_contacts)
    EditText etContacts;
    @BindView(R.id.et_phone)
    EditText etPhone;
    @BindView(R.id.recycleView)
    RecyclerView recycleView;
    @BindView(R.id.tv_last_step)
    TextView tvLastStep;
    @BindView(R.id.tv_next)
    TextView tvNext;
    @BindView(R.id.titleLine)
    View titleLine;
    @BindView(R.id.tv_start_time)
    TextView tvStartTime;
    @BindView(R.id.tv_end_time)
    TextView tvEndTime;
    private CheckBoxAdapter checkBoxAdapter;

    private AddSignTwo addSignTwo = new AddSignTwo();
    private int customersId;
    private Integer performanceId;
    private int signType;

    @Override
    protected AddSignPresenter createPresenter() {
        return new AddSignPresenter(this);
    }

    @Override
    protected int provideContentViewId() {
        return R.layout.activity_add_sign_two;
    }


    @Override
    public void initView() {
        super.initView();

        setTitleName("签约详情");
        List<String> titles = new ArrayList<>();
        titles.add("老顾客及朋友介绍");
        titles.add("网络");
        titles.add("上门");
        titles.add("其他");
        checkBoxAdapter = new CheckBoxAdapter(this);
        recycleView.setLayoutManager(new GridLayoutManager(this, 3));
        recycleView.setAdapter(checkBoxAdapter);
        checkBoxAdapter.setNewData(titles);


        checkBoxAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                checkBoxAdapter.setSelect(position);
                addSignTwo.setOwnerSource(String.valueOf(position + 1));
            }
        });
    }

    @Override
    public void initData() {
        super.initData();
        customersId = getIntent().getIntExtra("customersId", -1);
        performanceId = getIntent().getIntExtra("performanceId", -1);
        signType = getIntent().getIntExtra("type", -1);

        if (signType == 1) {

            addSignTwo.setOwnerSource("1");
        } else {
            mPresenter.getSingnContractdetail(performanceId);
        }
    }


    @RequiresApi(api = Build.VERSION_CODES.N)
    private void showDateDialog(TextView textView, int type) {
        //获取日历的一个实例，里面包含了当前的年月日
        Calendar calendar = Calendar.getInstance();
        DatePickerDialog dialog = new DatePickerDialog(this, new DatePickerDialog.OnDateSetListener() {
            @Override
            public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
                textView.setText(year + "-" + month + "-" + dayOfMonth);
                if (type == 1) {
                    addSignTwo.setTradingSignDate(textView.getText().toString() + " 00:00:00");
                } else if (type == 2) {
                    addSignTwo.setTradingPaymentDate(textView.getText().toString() + " 00:00:00");
                } else if (type == 3) {
                    addSignTwo.setTradingBeginTime(textView.getText().toString() + " 00:00:00");
                } else {
                    addSignTwo.setTradingEndTime(textView.getText().toString() + " 00:00:00");

                }
            }
        },
                calendar.get(Calendar.YEAR),
                calendar.get(Calendar.MONTH),
                calendar.get(Calendar.DAY_OF_MONTH));
        dialog.show();

    }

    @Override
    public void getPassengerNumberListSuccess(List<PassengerNumberBean> list) {

    }

    @Override
    public void saveOneStepSuccess(BaseData baseData) {

    }

    @Override
    public void saveTwoStepSuccess(BaseData baseData) {
       /* SaveBean saveBean = new Gson().fromJson(resultResponse.data.toString(), SaveBean.class);
        String performanceId = saveBean.getPerformanceId();
        String s = MathUtils.rvZeroAndDot(performanceId);
        SPUtils.getInstance(this).putString("performanceId", s);*/
        //跳转第二部
        Intent intent = new Intent(this, AddSignThreeActivity.class);
        startActivity(intent);

    }

    @Override
    public void saveThreeStepSuccess(BaseData baseData) {

    }

    @Override
    public void getSingnContractdetail(SignDetailBean signContractDetailBean) {

//        addSignTwo.setOwnerSource(String.valueOf(Integer.parseInt(signContractDetailBean.getOwnerSource()) -1));
        addSignTwo.setOwnerSource("1");
        String[] s = signContractDetailBean.getTradingSignDate().split(" ");
        addSignTwo.setTradingSignDate(signContractDetailBean.getTradingSignDate());
        tvDate.setText(s[0]);
        addSignTwo.setTradingPayment(signContractDetailBean.getTradingPayment());
        tvPayType.setText(signContractDetailBean.getTradingPayment());
        addSignTwo.setTradingPaymentDate(signContractDetailBean.getTradingPaymentDate());

        tvJiaofangTime.setText(signContractDetailBean.getTradingPaymentDate().split(" ")[0]);
        tvRent.setText(signContractDetailBean.getTradingVolRent());
        addSignTwo.setTradingVolRent(signContractDetailBean.getTradingVolRent());
        tvStartTime.setText(signContractDetailBean.getTradingBeginTime().split(" ")[0]);
        tvEndTime.setText(signContractDetailBean.getTradingEndTime().split(" ")[0]);
        addSignTwo.setTradingBeginTime(signContractDetailBean.getTradingBeginTime());
        addSignTwo.setTradingEndTime(signContractDetailBean.getTradingEndTime());

        etOwnerName.setText(signContractDetailBean.getOwnerLandlordName());
        addSignTwo.setOwnerLandlordName(signContractDetailBean.getOwnerLandlordName());
        addSignTwo.setOwnerContact(signContractDetailBean.getOwnerContact());
        etContacts.setText(signContractDetailBean.getOwnerContact());
        addSignTwo.setOwnerPhone(signContractDetailBean.getOwnerPhone());
        etPhone.setText(signContractDetailBean.getOwnerPhone());

    }

    @Override
    public void exChangeSignContractSuccess(BaseData data) {

    }


    @RequiresApi(api = Build.VERSION_CODES.N)
    @OnClick({R.id.tv_start_time,R.id.tv_jiaofang_time,R.id.tv_date, R.id.tv_end_time, R.id.tv_last_step,R.id.tv_next})
    public void onViewClicked(View view) {
        switch (view.getId()) {

            case R.id.tv_date:
                showDateDialog(tvDate, 1);
                break;
            case R.id.tv_jiaofang_time:
                showDateDialog(tvJiaofangTime, 2);

                break;
            case R.id.tv_start_time:
                showDateDialog(tvStartTime, 3);
                break;
            case R.id.tv_end_time:
                showDateDialog(tvEndTime, 4);
                break;
            case R.id.tv_last_step:
                Intent intent = new Intent(this,AddSignActivityOne.class);
                intent.putExtra("customersId", customersId);
                intent.putExtra("performanceId", performanceId);
                intent.putExtra("type", signType);
                startActivity(intent);
                finish();
                break;
            case R.id.tv_next:
                if (TextUtils.isEmpty(tvDate.getText().toString())) {
                    AppUtils.showToast("请选择日期");
                    return;
                }
                if (TextUtils.isEmpty(tvPayType.getText().toString())) {
                    AppUtils.showToast("请输入支付方式");
                    return;
                }
                if (TextUtils.isEmpty(tvJiaofangTime.getText().toString())) {
                    AppUtils.showToast("请选择交房日期");
                    return;
                }
                if (TextUtils.isEmpty(tvRent.getText().toString())) {
                    AppUtils.showToast("请输入租金");
                    return;
                }
                if (TextUtils.isEmpty(tvStartTime.getText().toString())) {
                    AppUtils.showToast("请选择开始时间");
                    return;
                }
                if (TextUtils.isEmpty(tvEndTime.getText().toString())) {
                    AppUtils.showToast("请选择结束时间");
                    return;
                }
                if (TextUtils.isEmpty(etOwnerName.getText().toString())) {
                    AppUtils.showToast("请输入姓名");
                    return;
                }
                if (TextUtils.isEmpty(etContacts.getText().toString())) {
                    AppUtils.showToast("请输入联系人姓名");
                    return;
                }
                if (TextUtils.isEmpty(etPhone.getText().toString())) {
                    AppUtils.showToast("请输入电话号");
                    return;
                }
                if (etPhone.getText().toString().length() != 11) {
                    AppUtils.showToast("电话号不正确");
                    return;
                }
                addSignTwo.setTradingPayment(tvPayType.getText().toString());
                addSignTwo.setTradingVolRent(tvRent.getText().toString().trim());
                addSignTwo.setOwnerLandlordName(etOwnerName.getText().toString());
                addSignTwo.setOwnerContact(etContacts.getText().toString());
                addSignTwo.setOwnerPhone(etPhone.getText().toString());
//                String performanceId = SPUtils.getInstance(this).getString("performanceId", "");
                addSignTwo.setPerformanceId(String.valueOf(performanceId));
                Log.e("addSignTwo", new Gson().toJson(addSignTwo));
                mPresenter.saveTwoStep(addSignTwo);
                break;

        }
    }

}